package com.rdc.dictionary.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Http访问工具类
 *
 * 主要用于把网络mp3解析到本地
 */
public class HttpUtil {
	/**
	 * 在新线程中发送网络请求
	 *
	 * @param address  网络地址
	 * @param listener HttpCallBackListener接口的实现类;
	 *                 onFinish方法为访问成功后的回调方法;
	 *                 onError为访问不成功时的回调方法
	 */
	public static void sentHttpRequest(final String address, final HttpCallBackListener listener) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				HttpURLConnection connection = null;
				try {
					URL url = new URL(address);
					connection = (HttpURLConnection) url.openConnection();
					connection.setRequestMethod("GET");
					connection.setConnectTimeout(8000);
					connection.setReadTimeout(8000);
					InputStream inputStream = connection.getInputStream();
					if (listener != null) {
						listener.onFinish(inputStream);
					}
				} catch (IOException e) {
					e.printStackTrace();
					if (listener != null) {
						listener.onError();
					}
				} finally {
					if (connection != null) {
						connection.disconnect();
					}
				}
			}
		}).start();
	}


	/**
	 * Http访问的回调接口
	 */
	public interface HttpCallBackListener {

		//当Http访问完成时回调onFinish方法
		void onFinish(InputStream inputStream);

		//当Http访问失败时回调onError方法
		void onError();
	}
}